//
//  HttpsViewController.m
//  AFN_Https_Test
//
//  Created by Adiljan on 2016/11/3.
//  Copyright © 2016年 Adiljan. All rights reserved.
//

#import "HttpsViewController.h"
#import "AfNetworking.h"
#import "MJExtension.h"

#define Url1 @"http://202.100.177.31:8787/ug/v1/rsa/create-rsa?id=1&name=abdirxat&phone=15276636332&gender=1"
#define Url2 @"https://202.100.177.31/ug/v1/city/list"

#define certificate @"mzl-server"
/**
 *  是否开启https SSL 验证
 *
 *  @return YES为开启，NO为关闭
 */
#define openHttpsSSL YES
@interface HttpsViewController ()
@property (strong, nonatomic) UIButton *btnData;
@end

@implementation HttpsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _btnData = [[UIButton alloc]initWithFrame:CGRectMake(70, 80, 60, 30)];
    [_btnData setTitle:@"点击" forState:0];
    _btnData.backgroundColor = [UIColor blueColor];
    _btnData.layer.cornerRadius = 5;
    _btnData.layer.masksToBounds = YES;
    [_btnData addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btnData];
    
}
- (void)btnClick{
//    [self getInfo];
    [self getAF];
}

-(void)getAF{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 
    // 2.申明返回的结果是text/html类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
//
//    manager.responseSerializer =[AFPropertyListResponseSerializer serializer];
//
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
    
    AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    policy.allowInvalidCertificates = YES;
    policy.validatesDomainName = YES;
    // 证书的路径
    // /先导入证书
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:certificate ofType:@"cer"];//证书的路径
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    
    manager.securityPolicy.pinnedCertificates = [[NSArray alloc] initWithObjects:certData, nil];
    // 客户端是否信任非法证书
    manager.securityPolicy.allowInvalidCertificates = YES;
    // 是否在证书域字段中验证域名
    [manager.securityPolicy setValidatesDomainName:NO];
    
    // 3.设置超时时间为5s
    manager.requestSerializer.timeoutInterval = 10;

    
    [manager GET:Url2 parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSLog(@"===============%@",responseObject);
//        NSDictionary *dictData = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
//        NSLog(@"===============%@",dictData);
        
        
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"-------------失败");
        
    }];
}
-(void)getInfo{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer =[AFPropertyListResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
    
    [manager GET:Url1 parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSLog(@"[responseObject description]--->%@",[responseObject description]);
        NSString *strRsa =[responseObject description];
        
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"-------------失败");
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
